package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageBean;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     *
     * @param dto dto
     * @return pageBean
     */
    @Override
    public PageBean pageList(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageList(dto);
        return PageBean.builder()
                .total(page.getTotal())
                .records(page.getResult())
                .build();
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO dto
     */
    @Override
    public void add(SetmealDTO setmealDTO) {
        // 新增套餐,关系到俩张表,一个是套餐表,另外一个是套餐菜品关系表
        // 添加套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 套餐默认状态为0
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insert(setmeal);
        // 添加套餐内的菜品信息 ,一个套餐中有多个菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        });
    }

    /**
     * 根据id查询套餐
     *
     * @param id 套餐id
     * @return vo
     */
    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = setmealMapper.selectById(id);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO dto
     */
    @Override
    public void updateWithDish(SetmealDTO setmealDTO) {
        // 修改套餐信息, 一个套餐对应多个菜品,先把原来套餐内的菜品全部干掉,再重新添加
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        // 套餐菜品表
        // 先删后加(根据套餐的id删除对应套餐下面的所有菜品,然后重新添加菜品)
        setmealDishMapper.deleteBySetMealId(setmeal.getId());
        log.info("更新的套餐id:{}", setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        });
    }

    /**
     * 套餐起售、停售
     *
     * @param status 套餐状态
     * @param id     套餐id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        // 套餐起售得保证其菜品全部起售
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        SetmealVO setmealVO = setmealMapper.selectById(id);
        // 得到其所有菜品
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            Long dishId = setmealDish.getDishId();
            Dish dish = dishMapper.selectById(dishId);
            if (dish.getStatus().equals(StatusConstant.DISABLE)) {
                throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        });
        setmealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐
     *
     * @param ids 套餐id集合
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        // 删除套餐,得更新俩个表,一个是setmeal表.,另一个是setmeal_dish表
        for (Long id : ids) {
            setmealMapper.deleteById(id);
            setmealDishMapper.deleteBySetMealId(id);
        }
    }

    /**
     * 根据分类id查询套餐(C端)
     *
     * @param categoryId 分类id
     * @return setmeal集合
     */
    @Override
    @Cacheable(cacheNames = "setmeal:list", key = "#categoryId")
    public List<Setmeal> selectByCategoryId(Long categoryId) {
        List<Setmeal> setmeals = setmealMapper.selectByCategoryId(categoryId);
        return setmeals;
    }

    /**
     * 根据套餐id查询包含的菜品
     *
     * @param id 套餐id
     * @return DishItemVO集合
     */
    @Override
    public List<DishItemVO> selectWithDishesByCategoryId(Long id) {
        return dishMapper.selectWithDishesByCategoryId(id);
    }
}
